package com.cloud.base.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * @Program: 反射demo
 * @Description:
 * @Version: v1.0.0
 * @Author: hantianyang
 * Modification History:
 * Date             Author      Version     Description
 * -------------------------------------------------------------
 * 2021/10/27  10:15     韩天阳      v1.0.0      创建
 */

public class ReflectionMain {
    public static void main(String[] args) throws Exception {

        //反射的举例
        //①
        Class<UserEntity> userEntityClass = UserEntity.class;
        UserEntity userEntity = userEntityClass.newInstance();
        System.out.println(userEntity);
        //②类的全路径名  包名+类名
        Class<?> aClass = Class.forName("com.cloud.base.reflection.UserEntity");
        Object o = aClass.newInstance();
        //无论是通过反射的哪种方式创建的对象都是通过字节码获取到的
        System.out.println(aClass == userEntityClass);
        //③
        Class<? extends UserEntity> aClass1 = new UserEntity().getClass();
        System.out.println(aClass == aClass1);
        UserEntity userEntity1 = aClass1.newInstance();
        System.out.println(userEntity == userEntity1);


        //反射获取对象的两种方法无参构造函数
        Class<UserEntity> a1 = (Class<UserEntity>) Class.forName("com.cloud.base.reflection.UserEntity");
        Constructor<UserEntity> declaredConstructor1 = a1.getDeclaredConstructor();
        declaredConstructor1.setAccessible(true);
        UserEntity userEntity2 = declaredConstructor1.newInstance();
        //反射获取对象的两种方法有参构造函数
        Class<UserEntity> a2 = (Class<UserEntity>) Class.forName("com.cloud.base.reflection.UserEntity");
        Constructor<UserEntity> declaredConstructor2 = a2.getDeclaredConstructor(new Class[]{String.class, String.class});
        UserEntity userEntity3 = declaredConstructor2.newInstance(new Object[]{"1", "2"});

        Class<UserEntity> a3 = (Class<UserEntity>) Class.forName("com.cloud.base.reflection.UserEntity");
        Field[] fields = a3.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        Class<UserEntity> a4 = (Class<UserEntity>) Class.forName("com.cloud.base.reflection.UserEntity");
        UserEntity userEntity4 = a4.newInstance();
        Field name = a4.getDeclaredField("name");
        name.setAccessible(true);
        name.set(userEntity4, "1");
        System.out.println(userEntity4.getName());

        Class<UserEntity> a5 = (Class<UserEntity>) Class.forName("com.cloud.base.reflection.UserEntity");
        UserEntity userEntity5 = a5.newInstance();
        Method getAge = a5.getDeclaredMethod("getAge", new Class[]{String.class, String.class});
        getAge.setAccessible(true);
        getAge.invoke(userEntity5, new Object[]{"1", "2"});

        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        Class<? extends ArrayList> aClass3 = strings.getClass();
        Method add = aClass3.getDeclaredMethod("add", new Class[]{Object.class});
        add.invoke(strings, 2);
        System.out.println(strings);
    }
}
